package it.polimi.TravelDream.web.beans;

import java.util.ArrayList;
import java.util.List;

import it.polimi.TravelDream.ejb.searchManager.HotelMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import it.polimi.TravelDream.ejb.dtos.*;

@ManagedBean(name = "HotelSearch")
@SessionScoped
public class HotelMgrBean {

	@EJB
	private HotelMgr hotelMgr;
	
	private List <HotelDTO> currentHotelList;
	private HotelDTO current;

	private int index;

	
	public HotelMgrBean(){
		this.currentHotelList = new ArrayList<HotelDTO>();
		this.current= new HotelDTO();
	}
	
	public String search(){
		hotelMgr.search();
		this.currentHotelList = hotelMgr.getHotelResultList(hotelMgr.getAddressResultList());
		return "search hotel results?faces-redirect=true";
	}

	public HotelMgr getHotelMgr() {
		return hotelMgr;
	}

	public void setHotelMgr(HotelMgr hotelMgr) {
		this.hotelMgr = hotelMgr;
	}

	public List<HotelDTO> getCurrentHotelList() {
		return currentHotelList;
	}

	public void setCurrentHotelList(List<HotelDTO> currentHotelList) {
		this.currentHotelList = currentHotelList;
	}
	
	public HotelDTO getCurrent() {
		return current;
	}

	public void setCurrent(HotelDTO current) {
		this.current = current;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String searchAll() {
		hotelMgr.searchAll();
		this.currentHotelList = hotelMgr.getHotelResultList(hotelMgr.getAddressResultList());
		return "search hotel results?faces-redirect=true";
	}
	
	public String goToUpdate(HotelDTO hotel) {
		this.current = hotel;
		this.index = currentHotelList.indexOf(hotel);
		return "update hotel?faces-redirect=true";
	}
	
	public String update() {
		hotelMgr.update(current, index);
		return "search hotel results?faces-redirect=true";
	}

}
